import jieba

from dict_model.WordFilter import filter_stop, count_deny, count_extent, count_polar


class Sentiment:

    def predict(self, sentence):
        # split sentence to word list
        word_gen = jieba.cut(sentence, cut_all=False)
        word_list = []
        for word in word_gen:
            word_list.append(word)
        # match
        words = filter_stop(word_list)
        deny = count_deny(words)
        extent = count_extent(words)
        polarity = count_polar(words)
        # score
        if deny % 2 == 1:
            deny = -1  # deny
        else:
            deny = 1
        return deny * (1 + extent) * polarity

    def test(self):
        cases = [
            '这本书非常垃圾',
            '好听得停不下来',
            '这首歌宛如天籁'
        ]
        for case in cases:
            result = self.predict(case)
            print(case, result)

if __name__ == '__main__':
    test()
